package com.homework.ch04;
import java.util.Scanner;
public class Test11 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入杨辉三角的高度: ");
        int height = scanner.nextInt();
        scanner.close();
        if (height <= 0) {
            System.out.println("高度必须大于0");
            return;
        }
        // 创建二维数组存储杨辉三角
        int[][] triangle = new int[height][];
        // 生成杨辉三角
        for (int i = 0; i < height; i++) {
            triangle[i] = new int[i + 1];
            // 每行的第一个和最后一个元素都是1
            triangle[i][0] = 1;
            triangle[i][i] = 1;
            // 计算中间的元素
            for (int j = 1; j < i; j++) {
                triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
            }
        }
        // 打印杨辉三角
        System.out.println("高度为 " + height + " 的杨辉三角:");
        for (int i = 0; i < height; i++) {
            // 打印前导空格，使三角居中
            for (int k = 0; k < height - i - 1; k++) {
                System.out.print("  ");
            }
            // 打印数字
            for (int j = 0; j <= i; j++) {
                System.out.printf("%4d", triangle[i][j]);
            }
            System.out.println();
        }

    }

}
